package com.ruoyi.eservice.service.impl;

import java.util.List;
import java.util.Arrays;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.base.BaseService;
import java.util.ArrayList;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.eservice.pojo.entity.NoticePlace;
import com.ruoyi.eservice.mapper.NoticePlaceMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.eservice.mapper.NoticeMapper;
import com.ruoyi.eservice.pojo.entity.Notice;
import com.ruoyi.eservice.service.INoticeService;
import com.ruoyi.eservice.pojo.dto.NoticeDto;

/**
 * 公告Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-11
 */
@Service
public class NoticeServiceImpl extends BaseService implements INoticeService
{
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private NoticePlaceMapper noticePlaceMapper;

    /**
     * 查询公告
     *
     * @param noticeId 公告主键
     * @return 公告
     */
    @Override
    public NoticeDto selectNoticeByNoticeId(Long noticeId)
    {
        return noticeMapper.selectNoticeByNoticeId(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告
     * @return 公告
     */
    @Override
    public List<NoticeDto> selectNoticeList(NoticeDto notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNotice(NoticeDto notice)
    {
        notice.setCreateBy(SecurityUtils.getUsername());
        notice.setCreateTime(DateUtils.getNowDate());
        notice.setUpdateBy(SecurityUtils.getUsername());
        notice.setUpdateTime(DateUtils.getNowDate());
        int rows = noticeMapper.insert(notice);
        insertNoticePlace(notice);
        return rows;
    }

    /**
     * 修改公告
     *
     * @param notice 公告
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNotice(NoticeDto notice)
    {
        notice.setUpdateBy(SecurityUtils.getUsername());
        notice.setUpdateTime(DateUtils.getNowDate());

        QueryWrapper<NoticePlace> queryWrapper = Wrappers.query();
        queryWrapper.lambda()
                .eq(NoticePlace::getNaNoticeId, notice.getNoticeId())
        ;
        noticePlaceMapper.delete(queryWrapper);
        insertNoticePlace(notice);
        return noticeMapper.updateById(notice);
    }

    /**
     * 批量删除公告
     *
     * @param noticeIds 需要删除的公告主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNoticeByNoticeIds(Long[] noticeIds)
    {
        QueryWrapper<NoticePlace> queryWrapper = Wrappers.query();
        queryWrapper.lambda()
                .in(NoticePlace::getNaNoticeId, Arrays.asList(noticeIds))
        ;
        noticePlaceMapper.delete(queryWrapper);
        return noticeMapper.deleteBatchIds(Arrays.asList(noticeIds));
    }

    /**
     * 删除公告信息
     *
     * @param noticeId 公告主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNoticeByNoticeId(Long noticeId)
    {
        QueryWrapper<NoticePlace> queryWrapper = Wrappers.query();
        queryWrapper.lambda()
                .eq(NoticePlace::getNaNoticeId, noticeId)
        ;
        noticePlaceMapper.delete(queryWrapper);
        return noticeMapper.deleteById(noticeId);
    }

    /**
     * 新增核酸点信息
     *
     * @param notice 公告对象
     */
    public void insertNoticePlace(NoticeDto notice)
    {
        List<NoticePlace> noticePlaceList = notice.getNoticePlaceList();
        Long noticeId = notice.getNoticeId();
        String createBy = notice.getCreateBy();
        Date createTime = notice.getCreateTime();
        String updateBy = notice.getUpdateBy();
        Date updateTime = notice.getUpdateTime();
        if (StringUtils.isNotNull(noticePlaceList))
        {
            for (NoticePlace noticePlace : noticePlaceList)
            {
                noticePlace.setNaNoticeId(noticeId);
                noticePlace.setCreateBy(createBy);
                noticePlace.setCreateTime(createTime);
                noticePlace.setUpdateBy(updateBy);
                noticePlace.setUpdateTime(updateTime);
                noticePlaceMapper.insert(noticePlace);
            }
        }
    }
}
